package com.winchain.chainbill.bill.biz.send.service.impl;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbSnd;
import com.winchain.chainbill.bill.biz.send.service.BillCrQuotaService;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.BillAcctService;
import com.winchain.chainbill.trade.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("billCrQuotaService")
public class BillCrQuotaServiceImpl implements BillCrQuotaService {
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private TradeService tradeService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;

	public void block(CbRec bill, CbSnd send) {
		String optType = "qt_blk";
		open(bill, send, optType);
	}

	public void unblock(CbRec bill, CbSnd send) {
		String optType = "qt_ubk";
		open(bill, send, optType);
	}

	private void open(CbRec bill, CbSnd send, String optType) {
		String bizType = "cr_open";
		String qtType = "1";

		AcctTxRec tx = getTxRec(bill, send, bizType, optType, qtType);

		tx.setAcctin(false);
		this.tradeService.callback(tx);
	}

	private AcctTxRec getTxRec(CbRec bill, CbSnd send, String bizType, String optType, String qtType) {
		AcctTxRec txRec = new AcctTxRec();
		txRec.setTxId(SystemUtil.getUUID());
		txRec.setTxSn(txRec.getTxId());
		txRec.setTxAmt(bill.getBillAmt());
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		String abs = "������������������������";
		if ("qt_ubk".equals(optType)) {
			abs = "����������������������������";
		}
		txRec.setTxAbs(abs);
		txRec.setTxType(bizType);
		txRec.setOptType(optType);
		txRec.setTxSts("S");
		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);
		txRec.setBizId(bill.getBillId());
		txRec.setDataScLvl("B");
		txRec.setCurrCode("CREDIT");
		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");
		txRec.setTxOperator(send.getBuyerId());

		AcctBas acct = this.billAcctService.getAcctBas(send.getBuyerId(), qtType, null);
		txRec.setTxAcctNo(acct.getAcctNo());
		txRec.setTxAcctNm(acct.getAcctNm());
		txRec.setTxBankNm(acct.getFiNm());
		AcctBkp bkp = this.billAcctBlcService.getCreditAcctBkpByCont(bill.getFinId(), send.getBuyerId());
		txRec.setTxBkpId(bkp.getBkpId());
		return txRec;
	}
}
